class_name ViewManager
extends Node2D

class ViewItem:
	var item_info:ViewItemInfo
	var view:BaseView
	var is_exist:bool
	var is_open:bool
	func _init(info:ViewItemInfo) -> void:
		item_info = info
		view = null
		is_exist = false # View对象是否已实例化
		is_open = false # View是否已打开
#region 属性
@export var view_item_infos:Array[ViewItemInfo]
@export var canvas_layer: CanvasLayer
var _view_dict:Dictionary = {}
var _cur_view_types:Array[GameEnum.ViewType] = []
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
#endregion
#region 公共方法
func init_manager():
	for info in view_item_infos:
		_view_dict[info.view_type] = ViewItem.new(info)
func get_view(view_type:GameEnum.ViewType) -> BaseView:
	if _view_dict.has(view_type):
		if not is_view_exist(view_type):
			var info:ViewItemInfo = _view_dict[view_type].item_info
			_view_dict[view_type].view = info.view.instantiate()
			_view_dict[view_type].is_exist = true
			canvas_layer.add_child(_view_dict[view_type].view)
		return _view_dict[view_type].view
	return null
func is_view_exist(view_type:GameEnum.ViewType) -> bool:
	if _view_dict.has(view_type):
		return _view_dict[view_type].is_exist
	return false
func is_view_open(view_type:GameEnum.ViewType) -> bool:
	if _view_dict.has(view_type):
		return _view_dict[view_type].is_open
	return false
func open_view(view_type:GameEnum.ViewType,args:Dictionary = {}):
	var view:BaseView = get_view(view_type)
	if view:
		if is_view_open(view_type):
			if view.view_reopen_mode == GameEnum.ReopenMode.Forbidden:
				return
			elif view.view_reopen_mode == GameEnum.ReopenMode.Reopen:
				_view_dict[view_type].is_open = false
				view.on_exit()
				_cur_view_types.erase(view_type)
				canvas_layer.remove_child(view)
				_exit_view_by_priprity(view.view_priority,view.view_open_mode)
				canvas_layer.add_child(view)
				_cur_view_types.append(view_type)
				view.on_enter(args)
				_view_dict[view_type].is_open = true
		else:
			canvas_layer.remove_child(view)
			_exit_view_by_priprity(view.view_priority,view.view_open_mode)
			canvas_layer.add_child(view)
			_cur_view_types.append(view_type)
			view.on_enter()
			_view_dict[view_type].is_open = true
func close_view(view_type:GameEnum.ViewType,args:Dictionary = {}):
	var view:BaseView = get_view(view_type)
	if view:
		if is_view_open(view_type):
			view.on_exit(args)
			_cur_view_types.erase(view_type)
			_view_dict[view_type].is_open = false
			if _cur_view_types.size() > 0:
				open_view(_cur_view_types[-1])
#endregion
#region 私有方法
func _exit_view_by_priprity(priority:int,open_mode:GameEnum.OpenMode):
	match open_mode:
		GameEnum.OpenMode.ExcludeLessPriority:
			for i in range(_cur_view_types.size()-1,-1,-1):
				var view_type:GameEnum.ViewType = _cur_view_types[i]
				var info:ViewItem = _view_dict[view_type]
				if is_view_exist(view_type) and info.view and info.view.view_priority < priority:
					info.view.on_exit()
					info.is_open = false
					_cur_view_types.erase(view_type)
		GameEnum.OpenMode.ExcludeLessEqualPriority:
			for i in range(_cur_view_types.size()-1,-1,-1):
				var view_type:GameEnum.ViewType = _cur_view_types[i]
				var info:ViewItem = _view_dict[view_type]
				if is_view_exist(view_type) and info.view and info.view.view_priority <= priority:
					info.view.on_exit()
					info.is_open = false
					_cur_view_types.erase(view_type)
		GameEnum.OpenMode.ExcludeEqualPriority:
			for i in range(_cur_view_types.size()-1,-1,-1):
				var view_type:GameEnum.ViewType = _cur_view_types[i]
				var info:ViewItem = _view_dict[view_type]
				if is_view_exist(view_type) and info.view and info.view.view_priority == priority:
					info.view.on_exit()
					info.is_open = false
					_cur_view_types.erase(view_type)
		GameEnum.OpenMode.ExcludeGreaterPriority:
			for i in range(_cur_view_types.size()-1,-1,-1):
				var view_type:GameEnum.ViewType = _cur_view_types[i]
				var info:ViewItem = _view_dict[view_type]
				if is_view_exist(view_type) and info.view and info.view.view_priority > priority:
					info.view.on_exit()
					info.is_open = false
					_cur_view_types.erase(view_type)
		GameEnum.OpenMode.ExcludeGreaterEqualPriority:
			for i in range(_cur_view_types.size()-1,-1,-1):
				var view_type:GameEnum.ViewType = _cur_view_types[i]
				var info:ViewItem = _view_dict[view_type]
				if is_view_exist(view_type) and info.view and info.view.view_priority >= priority:
					info.view.on_exit()
					info.is_open = false
					_cur_view_types.erase(view_type)
		GameEnum.OpenMode.ExcludeNone:
			return
		GameEnum.OpenMode.ExcludeAll:
			for i in range(_cur_view_types.size()-1,-1,-1):
				var view_type:GameEnum.ViewType = _cur_view_types[i]
				var info:ViewItem = _view_dict[view_type]
				if is_view_exist(view_type) and info.view:
					info.view.on_exit()
					info.is_open = false
					_cur_view_types.erase(view_type)
#endregion
#region 生命周期函数
#endregion
